import { LocaleState, LocaleStorage } from '@2/app/core/storage/locale.storage';
import { injectable } from 'inversify';
import { LocaleSetting, LocaleType } from '#/config';
import { Store } from 'pinia';
import { localeDefineStore } from '@2/app/core/storage/define/locale.define';
import { LOCALE_KEY } from '@/enums/cacheEnum';
import { localeSetting } from '@/settings/localeSetting';
import { createLocalStorage } from '@/utils/cache';
import 'reflect-metadata';

const ls = createLocalStorage();
/**
 * @program: microservice-central-portal
 * @description: 本地国际化存储接口实现
 * @author: baiHoo.chen
 * @create: 2022年5月26日
 **/
@injectable()
export class LocaleStorageImpl implements LocaleStorage {
  private readonly localeStore: Store<string, LocaleState>;
  constructor() {
    this.localeStore = localeDefineStore();
    this.localeStore.localInfo = (ls.get(LOCALE_KEY) || localeSetting) as LocaleSetting;
  }
  destroy(): void {}

  getLocale(): LocaleType {
    return this.localeStore.localInfo?.locale ?? 'zh_CN';
  }

  getShowPicker(): boolean {
    return !!this.localeStore.localInfo?.showPicker;
  }

  initLocale(): void {
    this.setLocaleInfo({
      ...localeSetting,
      ...this.localeStore.localInfo,
    });
  }

  setLocaleInfo(info: Partial<LocaleSetting>): void {
    this.localeStore.localInfo = { ...this.localeStore.localInfo, ...info };
    ls.set(LOCALE_KEY, this.localeStore.localInfo);
  }

  useDefineStore(): Store<string, LocaleState> {
    return this.localeStore;
  }
}
